package com.ruoyi.flowable.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.flowable.domain.bo.DraftBo;
import com.ruoyi.flowable.domain.vo.DraftVo;
import com.ruoyi.flowable.service.IDraftService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 表单草稿
 *
 * @author zxw
 * @date 2023-05-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/flowable/draft")
public class DraftController extends BaseController {

    private final IDraftService iDraftService;

    /**
     * 查询表单草稿列表
     */
    @SaCheckPermission("flowable:draft:list")
    @GetMapping("/list")
    public TableDataInfo<DraftVo> list(DraftBo bo, PageQuery pageQuery) {
        String userName = LoginHelper.getUsername();
        bo.setUserName(userName);
        return iDraftService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取表单草稿详细信息
     *
     * @param draftId 主键
     */
    @SaCheckPermission("flowable:draft:query")
    @GetMapping("/{draftId}")
    public R<DraftVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long draftId) {
        return R.ok(iDraftService.queryById(draftId));
    }

    /**
     * 新增表单草稿
     */
    @SaCheckPermission("flowable:draft:add")
    @Log(title = "表单草稿", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DraftBo bo) {
        String userName = LoginHelper.getUsername();
        bo.setUserName(userName);
        return toAjax(iDraftService.insertByBo(bo));
    }

    /**
     * 删除表单草稿
     *
     * @param draftIds 主键串
     */
    @SaCheckPermission("flowable:draft:remove")
    @Log(title = "表单草稿", businessType = BusinessType.DELETE)
    @DeleteMapping("/{draftIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] draftIds) {
        return toAjax(iDraftService.deleteWithValidByIds(Arrays.asList(draftIds), true));
    }
}
